<template>
    <div>
        <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
            <el-tab-pane label=同意/未分配 name="first">
                <div style="margin: 10px 0">
                    <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"
                              v-model="name"></el-input>
                    <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
                    <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
                    <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
                    <el-button type="warning" @click="reset">重置</el-button>
                </div>

                <div style="margin: 10px 0">
                    <el-button type="primary" @click="handlePost()">批量分配岗位 <i class="el-icon-edit"></i></el-button>
                    <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
                </div>

                <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                          @selection-change="handleSelectionChange" height="600px">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                    <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
                    <el-table-column prop="name" label="姓名" align="center" width="80"></el-table-column>
                    <el-table-column prop="sex" width="50" align="center" :formatter="sexFormatter"
                                     label="性别"></el-table-column>
                    <el-table-column prop="idcard" width="180" align="center" label="身份证号"></el-table-column>
                    <el-table-column prop="phone" align="center" width="100" label="手机"></el-table-column>
                    <el-table-column label="图片" align="center" width="150px">
                        <template slot-scope="scope">
                            <el-image style="width: 100px; height: 100px" :src="scope.row.avatar"
                                      :preview-src-list="[scope.row.avatar]"></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="representUnit" align="center" label="代表单位"></el-table-column>
                    <el-table-column prop="workUnit" align="center" :show-overflow-tooltip="true"
                                     label="工作单位"></el-table-column>
                    <el-table-column prop="judgeLevel" align="center" :formatter="levelFormatter"
                                     label="现裁判等级"></el-table-column>
                    <el-table-column prop="passDate" align="center" label="现等级通过时间"
                                     :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="latestRecord" :show-overflow-tooltip="true" align="center"
                                     label="最近执裁或复训活动名称及地点"></el-table-column>
                    <el-table-column prop="raceName" align="center" :show-overflow-tooltip="true"
                                     label="报名赛事"></el-table-column>
                    <el-table-column prop="raceId" align="center" label="赛事id" v-if="false"></el-table-column>
                    <el-table-column prop="isAccept" align="center" label="是否接受"
                                     :formatter="acceptFormatter"></el-table-column>
                    <el-table-column prop="post" align="center" label="选派岗位"
                                     :formatter="postFormatter"></el-table-column>
                    <el-table-column label="操作" width="220px" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" @click="handleEdit(scope.row)">选派岗位</el-button>
                            <el-button type="text" v-if="scope.row.isAccept ==='0'" @click="refuse(scope.row)"
                                       slot="reference">拒绝
                            </el-button>
                            <el-button type="text" v-else slot="reference" @click="refuse(scope.row)">接受</el-button>
                            <el-button type="text" @click="queryRecord(scope.row)">查看执裁记录</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div style="padding: 10px 0">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pageNum"
                            :page-sizes="[10, 30, 50, 100]"
                            :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </el-tab-pane>
            <el-tab-pane label="已拒绝" name="second">
                <div style="margin: 10px 0">
                    <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"
                              v-model="name"></el-input>
                    <el-button class="ml-5" type="primary" @click="loadRefuse">搜索</el-button>
                    <el-button type="warning" @click="reset">重置</el-button>
                </div>
                <div style="margin: 10px 0">
                    <el-button type="primary" @click="handlePost()">批量分配岗位 <i class="el-icon-edit"></i></el-button>
                </div>
                <el-table :data="refuseData" border stripe :header-cell-class-name="'headerBg'"
                          @selection-change="handleSelectionChange" height="600px">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                    <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
                    <el-table-column prop="name" label="姓名" align="center" width="80"></el-table-column>
                    <el-table-column prop="sex" width="50" align="center" :formatter="sexFormatter"
                                     label="性别"></el-table-column>
                    <el-table-column prop="idcard" width="180" align="center" label="身份证号"></el-table-column>
                    <el-table-column prop="phone" align="center" width="100" label="手机"></el-table-column>
                    <el-table-column label="图片" align="center" width="150px">
                        <template slot-scope="scope">
                            <el-image style="width: 100px; height: 100px" :src="scope.row.avatar"
                                      :preview-src-list="[scope.row.avatar]"></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="representUnit" align="center" label="代表单位"></el-table-column>
                    <el-table-column prop="workUnit" align="center" :show-overflow-tooltip="true"
                                     label="工作单位"></el-table-column>
                    <el-table-column prop="judgeLevel" align="center" :formatter="levelFormatter"
                                     label="现裁判等级"></el-table-column>
                    <el-table-column prop="passDate" align="center" label="现等级通过时间"
                                     :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="latestRecord" :show-overflow-tooltip="true" align="center"
                                     label="最近执裁或复训活动名称及地点"></el-table-column>
                    <el-table-column prop="raceName" align="center" :show-overflow-tooltip="true"
                                     label="报名赛事"></el-table-column>
                    <el-table-column prop="raceId" align="center" label="赛事id" v-if="false"></el-table-column>
                    <el-table-column prop="isAccept" align="center" label="是否接受"
                                     :formatter="acceptFormatter"></el-table-column>
                    <el-table-column prop="post" align="center" label="选派岗位"
                                     :formatter="postFormatter"></el-table-column>

                    <el-table-column label="操作" width="220px" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" @click="handleEdit(scope.row)">选派岗位</el-button>
                            <el-button type="text" v-if="scope.row.isAccept ==='0'" @click="refuse(scope.row)"
                                       slot="reference">拒绝
                            </el-button>
                            <el-button type="text" v-else slot="reference" @click="refuse(scope.row)">接受</el-button>
                            <el-button type="text" @click="queryRecord(scope.row)">查看执裁记录</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div style="padding: 10px 0">
                    <el-pagination
                            @size-change="handleSizeChange1"
                            @current-change="handleCurrentChange1"
                            :current-page="pageNum1"
                            :page-sizes="[10, 30, 50, 100]"
                            :page-size="pageSize1"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total1">
                    </el-pagination>
                </div>
            </el-tab-pane>
        </el-tabs>

        <el-dialog title="选派岗位" :visible.sync="dialogPostFormVisible" :close-on-click-modal="false" append-to-body>
            <template>
                <el-radio-group v-model="post">
                    <el-radio :label="0">技术代表</el-radio>
                    <el-radio :label="1">仲裁</el-radio>
                    <el-radio :label="2">裁判长</el-radio>
                    <el-radio :label="3">副裁判长</el-radio>
                    <el-radio :label="4">裁判员</el-radio>
                </el-radio-group>
            </template>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogPostFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="selectPost">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="执裁记录" :visible.sync="dialogTableVisible" append-to-body>
            <el-table :data="recordData">
                <el-table-column property="raceName" label="赛事名称" width="150"></el-table-column>
                <el-table-column property="raceStation" label="分站" width="200"></el-table-column>
                <el-table-column property="raceLevel" label="赛事级别"></el-table-column>
                <el-table-column property="racePost" :formatter="postFormatter" label="担任职位"></el-table-column>
            </el-table>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "Judge",
        data() {
            return {
                tableData: [],
                refuseData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                total1: 0,
                pageNum1: 1,
                pageSize1: 10,
                name: "",
                form: {},
                dialogTableVisible: false,
                dialogFormVisible: false,
                dialogPostFormVisible: false,
                multipleSelection: [],
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                post: "",
                postData: {},
                recordData: [],//执裁记录,
                raceDate: '',
                userIdcard: "",
                raceNames: [],
                batchOrNot: false,
            }
        },
        props: ['raceId','activeName'],
        created() {
            this.load()
            this.loadRefuse()
        },
        methods: {
            load() {
                const data = {
                    "page": this.pageNum,
                    "pageSize": this.pageSize,
                    "name": this.name,
                    "raceId": this.raceId
                }
                this.request.post("/judge/page", data).then(res => {
                    this.tableData = res.data.data
                    this.total = res.data.total
                })
            },
            loadRefuse(){
                const data = {
                    "page": this.pageNum1,
                    "pageSize": this.pageSize1,
                    "name": this.name,
                    "raceId": this.raceId
                }
                this.request.post("/judge/pageRefuse", data).then(res => {
                    this.refuseData = res.data.data
                    this.total1 = res.data.total
                })
            },
            handleClick(tab, event) {
                if(tab.name ==="first"){
                    this.load()
                }else if(tab.name ==='second'){
                    this.loadRefuse()
                }
            },
            loadRecord() {
                const data = {
                    "raceDate": this.raceDate,
                    "userIdcard": this.userIdcard
                    // "raceName":this.raceName
                }
                this.request.post("/judge/queryRecord", JSON.stringify(data)).then(res => {
                    if (res.code === 200) {
                        this.recordData = res.data
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            postFormatter(row) {
                let post = "";
                //兼容执裁记录的字段racePost
                if (row.post === undefined) {
                    post = row.racePost
                } else {
                    post = row.post
                }
                switch (post) {
                    case "0":
                        return "技术代表"
                        break;
                    case "1":
                        return "仲裁"
                        break;
                    case "2":
                        return "裁判长"
                        break;
                    case "3":
                        return "副裁判长"
                        break;
                    case "4":
                        return "裁判员"
                        break;
                }
            },
            acceptFormatter(row) {
                if (row.isAccept === "0") {
                    return "同意"
                } else if (row.isAccept === "1") {
                    return "拒绝"
                }
            },
            sexFormatter(row) {
                if (row.sex === "0") {
                    return "男"
                } else {
                    return "女"
                }
            },
            levelFormatter(row) {
                switch (row.judgeLevel) {
                    case "0":
                        return "国际级"
                        break;
                    case "1":
                        return "国家级"
                        break;
                    case "2":
                        return "预备国家级"
                        break;
                }
            },
            save() {
                this.request.post("/judge/save", this.form).then(res => {
                    if (res.code === 200) {
                        this.$message.success(res.message)
                        this.dialogFormVisible = false
                        this.load()
                        this.loadRefuse()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 查询执裁记录
             * @param row
             */
            queryRecord(row) {
                this.dialogTableVisible = true
                const data = {
                    "raceDate": this.raceDate,
                    "userIdcard": row.idcard
                    // "raceName":this.raceName
                }
                this.request.post("/judge/queryRecord", JSON.stringify(data)).then(res => {
                    if (res.code === 200) {
                        this.recordData = res.data
                        this.userIdcard = res.data[0].userIdcard
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            handleEdit(row) {
                this.$confirm('选派岗位默认接受报名, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // if(row.isAccept ==="1"){
                    //   return  this.$message.error("被拒绝的无法选派岗位！")
                    // }
                    this.batchOrNot = false
                    this.dialogPostFormVisible = true
                    this.postData = {
                        "id": row.id,
                        "idcard": row.idcard,
                        "raceId": row.raceId,
                        "raceName": row.raceName
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });


            },
            /**
             * 选派岗位
             */
            selectPost() {
                //true为批量
                if (this.batchOrNot) {
                    this.batchPost()
                } else {
                    this.postData.racePost = this.post;
                    this.postData.isAccept = "0";
                    this.postData.flag = "post";
                    this.request.post("/judge/save", JSON.stringify(this.postData)).then(res => {
                        if (res.code === 200) {
                            this.$message.success("分配岗位成功")
                            this.dialogPostFormVisible = false
                            this.load()
                            this.loadRefuse()
                        } else {
                            this.$message.error("操作失败")
                        }
                    })
                }

            },
            refuse(row) {
                let isAccept = ""
                if (row.isAccept === "0") {
                    isAccept = "1";
                    this.$confirm('拒绝该裁判员将不再显示此报名记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        const data = {
                            "id": row.id,
                            "isAccept": isAccept,
                            "post": "",//置空岗位
                            "flag": "isAccept",
                            "idcard": row.idcard,
                            "raceId": row.raceId
                        }
                        this.request.post("/judge/save", JSON.stringify(data)).then(res => {
                            if (res.code === 200) {
                                this.$message.success("操作成功")
                                this.load()
                                this.loadRefuse()
                            } else {
                                this.$message.error("操作失败")
                            }
                        })
                    })
                } else {
                    isAccept = "0"
                    const data = {
                        "id": row.id,
                        "isAccept": isAccept,
                        "post": "",//置空岗位
                        "flag": "isAccept",
                        "idcard": row.idcard,
                        "raceId": row.raceId
                    }
                    this.request.post("/judge/save", JSON.stringify(data)).then(res => {
                        if (res.code === 200) {
                            this.$message.success("操作成功")
                            this.load()
                            this.loadRefuse()
                        } else {
                            this.$message.error("操作失败")
                        }
                    })
                }
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            handlePost() {
                this.$confirm('选派岗位默认接受报名, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.batchOrNot = true
                    this.dialogPostFormVisible = true
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },

            batchPost() {
                if (!this.multipleSelection.length) {
                    this.$message.error("请选择需要分配的裁判")
                    return
                }
                this.dialogPostFormVisible = true

                let ids = this.multipleSelection.map(v => v.idcard)  // [{}, {}, {}] => [1,2,3]
                const data = {
                    "raceId": this.raceId,
                    "idcards": ids,
                    "post": this.post,
                    "isAccept": "0"
                }
                this.request.post("/judge/batchUpdatePost", data).then(res => {
                    if (res.code === 200) {
                        this.$message.success(res.message)
                        this.dialogPostFormVisible = false
                        this.load()
                        this.loadRefuse()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            reset() {
                this.name = ""
                this.load()
                this.loadRefuse();
            },
            recordReset() {
                this.raceDate = ""
                this.loadRecord()
            },
            handleSizeChange(pageSize) {
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                this.pageNum = pageNum
                this.load()
            },
            handleSizeChange1(pageSize) {
                this.pageSize1 = pageSize
                this.load()
            },
            handleCurrentChange1(pageNum) {
                this.pageNum1 = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                this.form.file = res
            },
            handleImgUploadSuccess(res) {
                this.form.img = res
            },
            download(url) {
                window.open(url)
            },
            exp() {
                window.open(this.GLOBAL.baseURL + "rank/judge/export/" + this.raceId)
            },
            handleExcelImportSuccess() {
                this.$message.success("导入成功")
                this.load()
            }
        }
    }
</script>


<style>
    .headerBg {
        background: #eee !important;
    }

    .el-button {
        margin: 0px 6px 12px;
    }


</style>
